//
//  ECUIVC_Mine.m
//  Edu_iOS2018
//
//  Created by apple on 2017/12/28.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUIVC_Mine.h"
#import "UIStory_CLogin.h"
#import "ECUIVC_Login.h"
#import "CYAnimation.h"

#import "ECMineTableViewCell.h"
#import "ECMineSettingViewController.h"
#import "ECMineProfileViewController.h"

#import "CYDataHelper.h"


@interface ECUIVC_Mine ()<UITableViewDataSource,UITableViewDelegate>{
    
    UILabel *nameLab;//姓名
    UILabel *subLab;//个性签名
    UIImageView *iconImageView;//头像
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation ECUIVC_Mine

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshMyProfile];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addNotification];
    
    //右上角设置按钮
    ECUButton *setBtn = [ECUButton buttonWithType:UIButtonTypeCustom];
    setBtn.frame = CGRectMake(0, 0, 25, 25);
    [setBtn setBackgroundImage:[UIImage imageNamed:@"Mine_set"] forState:0];
    [setBtn addTarget:self action:@selector(setBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightitem=[[UIBarButtonItem alloc]initWithCustomView:setBtn];
    self.navigationItem.rightBarButtonItem=rightitem;
    
    //添加tableview
    [self.view addSubview:self.tableView];
    
}

-(void)addNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifactionUserLogin) name:kNotifactionUserLogin object:nil];//注册登录成功通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifactionUserLogout) name:kNotifactionUserLogout object:nil];//注册退出登录通知
}

/*
 根据登录通知,更新用户名/个签/头像信息
 */
-(void)notifactionUserLogin{
    nameLab.text = [UserInfoManager shareUser].nickName;
    
    NSString *signAture = [UserInfoManager shareUser].signAture;
    if ([CYDataHelper isNull:signAture]) {
        signAture = @"暂无个性签名";
    }
    subLab.text = signAture;
    
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:[UserInfoManager shareUser].smallAvatar] placeholderImage:[UIImage imageNamed:@"Mine_iconImage"]];
}

/*
 根据登录通知,更新用户名/个签/头像信息
 */
-(void)notifactionUserLogout{
    nameLab.text = @"点击登录";
    subLab.text = @"暂无个性签名";
    iconImageView.image = [UIImage imageNamed:@"Mine_iconImage"];
}

-(void)refreshMyProfile{
    if ([[UserInfoManager shareUser]token]) {
        nameLab.text = [UserInfoManager shareUser].nickName;
        
        NSString *signAture = [UserInfoManager shareUser].signAture;
        if ([CYDataHelper isNull:signAture]) {
            signAture = @"暂无个性签名";
        }
        subLab.text = signAture;
        
        [iconImageView sd_setImageWithURL:[NSURL URLWithString:[UserInfoManager shareUser].smallAvatar] placeholderImage:[UIImage imageNamed:@"Mine_iconImage"]];
    }
}


/*  懒加载tableview  */
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavHeight-kTabbarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"f5f5f5"];
    }
    return _tableView;
}

/*  懒加载数据  */
-(NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[@[],@[@"我的课程"],@[@"课程订单",@"课程收藏",@"课程评价"],@[@"消息",@"账户安全"/*,@"意见反馈"*/]];
        _imageArray = @[@[],@[@"Mine_myCourse"],@[@"Mine_courseOrder",@"Mine_courseFav",@"MIne_courseSub"],@[@"Mine_message",@"Mine_safety"/*,@"Mine_feedback"*/]];
    }
    return _dataArray;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 1;
    }else if (section == 2){
        return 3;
    }else if (section == 3){
        return 2;
    }
    return 0;//分组区分
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;//共4组
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indetify = @"ECMineTableViewCell";
    ECMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetify];
    if (!cell) {
        cell = [[ECMineTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
    }
    //设置数据
    [cell setTitle:self.dataArray[indexPath.section][indexPath.row] andImage:self.imageArray[indexPath.section][indexPath.row] andIndexPath:indexPath];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //不同的Cell跳转到不同的页面
    if (![[UserInfoManager shareUser]token]) {
        UIStory_CLogin *uistory_login = [[UIStory_CLogin alloc] init];
        [self presentViewController:uistory_login.navigationController animated:YES completion:nil];
        return;
    }
    
//            [self.view.window.layer addAnimation:[CYAnimation cubeAnimationDuration:1 subType:kCATransitionFromRight] forKey:@""];
    NSArray *vcArray = @[@[@""],
                         @[@"ECMineCourseViewController"],
                         @[@"ECMineCourseOrderViewController",
                           @"ECMineFavViewController",
                           @"ECMineCourseSubViewController"],
                         @[@"ECMineMessageViewController",
                           @"ECMineSafetyViewController",
                           @"ECMineFeedBackViewController"]];
    UIViewController *vc =[NSClassFromString(vcArray[indexPath.section][indexPath.row]) new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kProportion_Height(144);
}

//设置分区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kProportion_Width(278);
    }
    return 0;
}
//设置分区头视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kProportion_Width(278))];
    headerview.backgroundColor = [UIColor whiteColor];
    //添加点击事件
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [headerview addGestureRecognizer:tapGesturRecognizer];
    
    nameLab = [[UILabel alloc]init];
    [headerview addSubview:nameLab];
    nameLab.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
    nameLab.text = [UserInfoManager shareUser].nickName;
    nameLab.font = [UIFont systemFontOfSize:15*kPercentage];
    
    subLab = [[UILabel alloc]init];
    [headerview addSubview:subLab];
    subLab.numberOfLines = 2;
    subLab.font = [UIFont systemFontOfSize:13*kPercentage];
    subLab.text = [UserInfoManager shareUser].signAture;
    subLab.textColor = [UIColor colorWithHexString:@"#999999"];
    
    if (![[UserInfoManager shareUser]token]) {
        nameLab.text = @"点击登录";
        subLab.text = @"暂无个性签名";
    }else{
        nameLab.text = [UserInfoManager shareUser].nickName;
        NSString *signAture = [UserInfoManager shareUser].signAture;
        if ([CYDataHelper isNull:signAture]) {
            signAture = @"暂无个性签名";
        }
        subLab.text = signAture;
    }
    
    iconImageView = [[UIImageView alloc]init];
    [headerview addSubview:iconImageView];
    iconImageView.layer.cornerRadius = kProportion_Width(90);
    iconImageView.layer.masksToBounds = YES;
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:[UserInfoManager shareUser].smallAvatar] placeholderImage:[UIImage imageNamed:@"Mine_iconImage"]];
    
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerview.mas_left).offset(kProportion_Width(45));
        make.top.equalTo(headerview.mas_top).offset(kProportion_Width(59));
        make.right.equalTo(subLab.mas_right);
        make.height.equalTo(@18);
    }];
    
    [subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLab.mas_left);
        make.top.equalTo(nameLab.mas_bottom).offset(kProportion_Width(18));
        make.right.equalTo(iconImageView.mas_left).offset(-kProportion_Width(90));
        make.bottom.equalTo(headerview.mas_bottom).offset(-kProportion_Width(40));
    }];
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headerview.mas_right).offset(-kProportion_Width(46));
        make.centerY.equalTo(headerview.mas_centerY);
        make.height.width.equalTo(@(kProportion_Width(180)));
    }];
    
    return headerview;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return kProportion_Width(30);
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    footerview.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    return footerview;
}

#pragma mark 右上角按钮点击事件
-(void)setBtnClick{
    ECMineSettingViewController *vc = [ECMineSettingViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/*  头部点击事件  */
-(void)tapAction{
    //判断是否登录
    if (![[UserInfoManager shareUser]token]) {
        UIStory_CLogin *uistory_login = [[UIStory_CLogin alloc] init];
        [self presentViewController:uistory_login.navigationController animated:YES completion:nil];
        return;
    }
    
    ECMineProfileViewController *vc = [[ECMineProfileViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:kNotifactionUserLogin object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:kNotifactionUserLogout object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
